//detail模块的小仓库

import {reqGoodsInfo,reqAddOrUpdate} from '@/api'
// 封装游客身份模块nanoId
import {getnanoId} from '@/utils/nanoid_token';

const actions ={
    async getGoodInfo({commit},skuId){
        let result = await reqGoodsInfo(skuId);
        if(result.code==200) {
            commit('GETGOODINFO',result.data)
        }
    },
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        let result = await reqAddOrUpdate(skuId,skuNum);
        if(result.code==200) return 'Ok';
        else return Promise.reject(new Error('修改失败'));

    }
};
//修改state的唯一手段
const mutations ={
    GETGOODINFO(state,goodInfo){
        state.goodInfo=goodInfo;
    }
};
const state ={
    goodInfo:{},
    nanoid_token:getnanoId()

};
//理解为计算属性，用于简化仓库数据，让组件获取仓库的数据更加方便
const getters ={
    categoryView(state){
        return state.goodInfo.categoryView  || {}
    },
    skuInfo(state){
        return state.goodInfo.skuInfo  || {}
    },
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList  || []
    }

};

export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}